package com.module.antiFake.manager;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;
import base.util.SQLUtil;

import com.module.antiFake.dao.AntiFakeLBSDao;
import com.module.antiFake.dao.AntiFakeScanHistoryDao;
import com.module.antiFake.vo.AntiFakeLBS;
import com.module.antiFake.vo.AntiFakeScanHistory;
import com.module.antiFake.vo.AppAttr;

/**
 * 扫码相关逻辑操作类
 * @author wult
 *
 */
public class AntiFakeScanHistoryManager extends GenericHibernateManager<AntiFakeScanHistory, Long>{
	private AntiFakeScanHistoryDao dao = AntiFakeScanHistoryDao.getInstance();
//	private AntiFakeProductRecallDao productRecallDao = AntiFakeProductRecallDao.getInstance();
	private AntiFakeLBSDao lbsDao = AntiFakeLBSDao.getInstance();
	private static AntiFakeScanHistoryManager service;
	private final static String  NOT_ADDERSS = "未知地点";
	public synchronized static AntiFakeScanHistoryManager getInstance() {
		if(service == null){
			service = new AntiFakeScanHistoryManager();
		}
		return service;
	}
    
	/**
	 * 保存手机扫码操作记录;
	 * @param antiFakeScanHistory
	 * @return
	 */
//	public boolean save(AntiFakeScanHistory antiFakeScanHistory) throws Exception{
//		Session session = this.getSession();
//		Transaction tx = session.beginTransaction();
//		boolean result = false;
//		//如果codeState为3,并且batchCodeState为2执行下一步操作;
//		if(antiFakeScanHistory.getCodeState().equals(new Integer(3))
//				&& antiFakeScanHistory.getBatchCodeState().equals(new Integer(2))){
//			try{
//				//检查此商品是否需要召回;
//				AntiFakeProductRecall productRecall = productRecallDao.getProductRecallByCode(session,antiFakeScanHistory.getFirstCodeInfo());
//				antiFakeScanHistory.setRecall(productRecall!=null);
//				if(antiFakeScanHistory.isRecall()){
//					antiFakeScanHistory.setRecallInfo(productRecall.getDesc());
//				}else{
//					codeLBSInfo(antiFakeScanHistory, session);
//					result = dao.save(session,antiFakeScanHistory);
//					tx.commit();
//				}
//			}catch (Exception e) {
//				tx.rollback();
//				throw e;
//			}finally{
//				this.closeSession();
//			}
//		}else{
//			result = false; 
//		}
//		return result;
//	}

	/**
	 * 处理扫码地理位置
	 * @param antiFakeScanHistory
	 * @param session
	 */
	private void codeLBSInfo(AntiFakeScanHistory antiFakeScanHistory,
			Session session) {
		AntiFakeLBS lbs = lbsDao.findLBSByLatitudeAndLongitude(session, antiFakeScanHistory.getLongitude(), antiFakeScanHistory.getLatitude());
		if(lbs != null){
			antiFakeScanHistory.setLbsId(lbs.getId());
		}
	}

	/**
	 * 方法取消不使用了！
	 * 获取当前码信息，
	 * 先从本库中的码列表中尝试读取码信息，
	 * 如果当前码列表中没有码相关信息，
	 * 则从码管理模块读取;
	 * @param antiFakeScanHistory
	 */
	public void checkCodeInfo(AntiFakeScanHistory antiFakeScanHistory) throws Exception{
		Session session = this.getSession();
		try{
			AntiFakeScanHistory oldHistory = dao.findUniqueObjectBySQL(session,"FROM AntiFakeScanHistory antiFakeScanHistory " +
					"WHERE antiFakeScanHistory.id = (SELECT max(history.id) FROM AntiFakeScanHistory history WHERE history.codeInfo='"+
					antiFakeScanHistory.getCodeInfo()+"')");
			//如未有扫码记录，从码管理模块读取;
			if(oldHistory == null){
				getCodeInfoFromCodeManagerModule(antiFakeScanHistory);
			}
			//如果当前码已刷过,则从数据库里面把其属性读出;
			else{
				antiFakeScanHistory.setFirstCodeInfo(oldHistory.getFirstCodeInfo());
				antiFakeScanHistory.setLayerNum(oldHistory.getLayerNum());
				antiFakeScanHistory.setOrgid(oldHistory.getOrgid());
				antiFakeScanHistory.setType(oldHistory.getType());
			}
		}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}	
	}
	
	/**
	 * 访问码管理模块,获取码信息;
	 * @param antiFakeScanHistory
	 */
	private void getCodeInfoFromCodeManagerModule(
			AntiFakeScanHistory antiFakeScanHistory) {
		antiFakeScanHistory.setType(AntiFakeScanHistory.ONE_LABEL);
		antiFakeScanHistory.setLayerNum(new Short("1"));
	}

	/**
	 * 扫码的历史记录;
	 * @param bean
	 */
//	public void listScanHistory(AntiFakeBeanUtil bean) throws Exception{
//		Session session = this.getSession();
//		try {
//			String hql="FROM AntiFakeScanHistory history WHERE history.codeInfo='"
//				+ bean.getAntiFakeScanHistory().getCodeInfo()
//				+ "' AND " + SQLUtil.getExistWhere("history")
//				+ " ORDER BY history.createtime DESC ";
//			List<AntiFakeScanHistory> historyList_temp = dao.findBySQL(session,hql);
//			List<AntiFakeScanHistory> historyList = new LinkedList();
//			int i = 0;
//			for(AntiFakeScanHistory o:historyList_temp)
//			{
//				if(i++ > 200)
//					break;
//				else
//					historyList.add(o);
//			}
//			
//			bean.setHistoryList(getHistoiryListLbs(session,historyList));
//		} catch (Exception e) {
//			throw e;
//		}finally{
//			this.closeSession();
//		}		
//	}
	
	/**
	 * 加载LBS地址
	 * @param list
	 * @return
	 * @throws Exception 
	 */
	private List<AntiFakeScanHistory> getHistoiryListLbs(Session session,List<AntiFakeScanHistory> list) throws Exception{
		List<AntiFakeScanHistory> listh = new ArrayList<AntiFakeScanHistory>();
		for(AntiFakeScanHistory afs : list){
			afs.setAdderss(NOT_ADDERSS);
			if(afs.getLatitude() != null && afs.getLatitude()>0){
				AntiFakeLBS lbs = lbsDao.findLBSByLatitudeAndLongitude(session,afs.getLongitude(),afs.getLatitude());
				if(lbs != null){
					afs.setAdderss(lbs.getProvince()+lbs.getCity()+lbs.getCounty());
				}
			}
			listh.add(afs);
		}
		return listh;
	} 
	

	/**
	 * 根据imei号列出扫码记录;
	 * @param bean
	 */
//	public void listScanHistoryFromImei(AntiFakeBeanUtil bean) throws Exception{
//		List<AntiFakeScanHistory> historyList = null;
//		Session session = this.getSession();
//		AntiFakeScanHistory history = bean.getAntiFakeScanHistory();
//		String hql = "FROM AntiFakeScanHistory history WHERE history.imei='"+history.getImei()+"' AND "+SQLUtil.getExistWhere("history")+" ORDER BY history.createtime DESC";
//		try {
//			if (history.getPageSize() > 0) {
//				historyList = dao.findObjectListsByPage(session, hql, 0,
//						history.getPageSize());
//			} else {
//				historyList = dao.findBySQL(session, hql);
//			}
//			bean.setHistoryList(getHistoiryListLbs(session,historyList));
//			//bean.setHistoryList(historyList);
//		} catch (Exception e) {
//			throw e;
//		}finally{
//			this.closeSession();
//		}	
//	}

	/**
	 * 通过网页查询条件，列出扫码记录
	 * @param bean
	 */ 
	public Page findScanHistoryFromWebSearch(AntiFakeScanHistory bean,Page page) throws Exception{
		try {
			return dao.getList(this.getSession(), bean,page);
		} catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}	
	}

	/**
	 * 根据扫码情况,更新应用属性;
	 * @param bean
	 */
//	public void reflashAttrApp(AntiFakeBeanUtil bean) {
//		List<AppAttr> list =  bean.getAppAttrList();
//		List<AppAttr> resultList = new ArrayList<AppAttr>();
//		AntiFakeScanHistory history = bean.getAntiFakeScanHistory();
//		if(list!=null){
//			for(AppAttr app : list){
//				//击穿;
//				if(history.getSecondScaned().equals(new Short("1"))){
//					//第一层;
//					if(history.getLayerNum().equals(new Short("1"))){
//						if(app.getAppType().equals(AppAttr.FIRST_CHECKOUTED)){
//							resultList.add(app);
//						}
//					}
//					//第二层
//					else{
//						if(app.getAppType().equals(AppAttr.SECOND_CHECKOUTED)){
//							resultList.add(app);
//						}
//					}
//				}
//				//未被击穿
//				else{
//					//第一层;
//					if(history.getLayerNum().equals(new Short("1"))){
//						if(app.getAppType().equals(AppAttr.FIRST_CHECKOUT)){
//							resultList.add(app);
//						}
//					}
//					//第二层
//					else{
//						if(app.getAppType().equals(AppAttr.SECOND_CHECKOUT)){
//							resultList.add(app);
//						}
//					}
//				}
//			}
//		}
//		bean.setAppAttrList(resultList);
//	}

	@Override
	public GenericHibernateDao<AntiFakeScanHistory, Long> getDao() {
		return dao;
	}
}
